package com.stu.spring.context.chapter10.tran;

import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.sql.Types;

public class MyBookServiceImpl implements MyBookService{

	private JdbcTemplate jdbcTemplate;

	// 设置数据源
	public void setDataSource(DataSource dataSource){
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	@Override
	public void save(MyBook myBook) {
		// 事务在 MyBookService 类上面加了
		jdbcTemplate.update("insert into my_book(name, price, note) values (?,?,?)",
				new Object[]{myBook.getName(), myBook.getPrice(), myBook.getNote()},
				new int[]{Types.VARCHAR, Types.INTEGER, Types.VARCHAR});

		throw new RuntimeException("save failer");
	}
}
